package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.model.User;
import cn.edu.ujs.coor.common.annotation.DataLevel;
import cn.edu.ujs.coor.repository.field.FieldRepository;
import cn.edu.ujs.coor.repository.item.ItemRepository;
import cn.edu.ujs.coor.request.IdRequest;
import cn.edu.ujs.coor.request.user.ResetPasswordRequest;
import cn.edu.ujs.coor.request.user.UserConfigRequest;
import cn.edu.ujs.coor.request.user.UserPageRequest;
import cn.edu.ujs.coor.request.user.UserUpdateRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.response.PageResponse;
import cn.edu.ujs.coor.response.UserResponse;
import cn.edu.ujs.coor.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户信息相关Controller
 *
 * @author 柳曾雄
 * @date 2018-07-16
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FieldRepository fieldRepository;

    @Autowired
    private ItemRepository itemRepository;

    /**
     * 分页查询用户信息
     * @param request
     * @return
     */
    @DataLevel
    @RequestMapping(value = "/list")
    public JsonResponse list (@RequestBody UserPageRequest request, User user) {
        PageResponse<UserResponse> response = userService.page(request);
        return JsonResponse.success(response);
    }

    /**
     * 用户详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail")
    public JsonResponse list (@RequestBody @Valid IdRequest request) {
        UserResponse response = userService.detail(request);
        return JsonResponse.success(response);
    }

    /**
     * 更新用户信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResponse update (@Valid @RequestBody UserUpdateRequest request, User user) {
        userService.update(request);
        return JsonResponse.success();
    }

    /**
     * 删除用户信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    public JsonResponse delete (@RequestBody IdRequest request) {
        userService.delete(request);
        return JsonResponse.success();
    }

    /**
     * 重置密码
     * @param request
     * @return
     */
    @RequestMapping(value = "/reset", method = RequestMethod.POST)
    public JsonResponse reset (@Valid @RequestBody ResetPasswordRequest request, User user) {
        request.setId(user.getId());
        userService.reset(request);
        return JsonResponse.success();
    }

    /**
     * 配置用户角色
     * @param request
     * @return
     */
    @RequestMapping(value = "/config", method = RequestMethod.POST)
    public JsonResponse config (@Valid @RequestBody UserConfigRequest request) {
        userService.config(request);
        return JsonResponse.success();
    }

    /**
     * 令牌信息提取
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public JsonResponse info (User user) {
        JSONObject info = userService.info(user);
        return JsonResponse.success(info);
    }
}
